<template>
  <div class="clearfix table-out">
    <div class="clearfix out-con pos-rel">
      <el-button
        type="primary"
        plain
        size="small"
        class="return pointer"
        @click="returnGo"
        ><i class="iconfont icon-fanhui"></i> 返回
      </el-button>
      <!--       <top-step ref="topStepRef"></top-step>-->
      <!--      <div class="clear h20"></div>-->
      <form-data ref="formDataRef" @getPdf="getPdf"></form-data>
      <div class="but-out clearfix">
        <el-button type="primary" class="submit-big two" @click="submit(3)"
          >暂存</el-button
        >
        <el-button type="primary" class="submit-big two" @click="submit(1)"
          >提交</el-button
        >
      </div>
      <form-data-pdf
        v-show="pdfStatic"
        :pdfTitle="pdfTitle"
        :pdf-title-sub="pdfTitleSub"
        :form-data="pdfData"
        ref="formDataPdfRef"
      ></form-data-pdf>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import pubJs from "./pub";

export default {
  mixins: [pubJs],

  data() {
    return {
      userInfo: JSON.parse(localStorage.getItem("userInfo")),
      saisjc: null,
      saisId: null,
      rulueStatic: false,
      saveId: "",
      userData:{}
    };
  },

  inject: ["imageUrl", "serviceUrl"],

  async mounted() {
    this.saveId = "";
    this.pdfStatic = false;
    this.saisjc = this.$route.query.saisjc;
    this.saisId = this.$route.query.id;
    let params = {
      saisjc: this.saisjc,
      saisId: this.saisId,
    };
    await this.getGerxxByUserName()
    this.$refs["formDataRef"].getFormDefine(params, "project2");
    // this.$refs["topStepRef"].getList(params, "project2");
  },

  methods: {
    // 读个人信息
    getGerxxByUserName(){
     let that = this
      axios
          .get(that.serviceUrl + "/gerzx2/cbda2Gerzx/getGerxxByUserName", {
            headers: {
              "X-Access-Token": localStorage.getItem("token"),
            },
            params: {
              createBy: that.userInfo.username
            },
          })
          .then((resp) => {
            if(resp&&resp.data.code==200){
              let params = resp.data.result
              for (let key in params) {
                if (params[key] !== null && params[key] !== "") {
                  if (
                      JSON.stringify(params[key]).includes("[") &&
                      JSON.stringify(params[key]).includes("]")
                  ) {
                    console.log(params[key],'params[key]');
                    params[key] = JSON.parse(params[key]);
                  }
                }
              }
              this.$refs["formDataRef"].userData = params
              this.$refs["formDataRef"].canssfValue = params.canssf;
            }
          })
          .catch((error) => {
            //console.log(error);
          });
    },
    submit01(num) {
      this.btnLoading = true
      console.log(num,'submit');
      this.pdfParams = {};
      let that = this;
      let _formData = that.$refs["formDataRef"].formData;
      let tableData = that.$refs["formDataRef"].tableData;
      if (_formData.dangqzjxm) {
        _formData.dangqzjxm = tableData;
      }
      const config = {
        headers: {
          "X-Access-Token": localStorage.getItem("token"),
        },
      };
      // 复用数值字段
      let tmp = {
        isExport: num == 1 ? false : true,
        id: this.saveId,
        saisjc: that.saisjc ? that.saisjc : "",
        saisId: that.saisId ? that.saisId : "",
        saisqyId: _formData.saiq,
        formData: _formData,
      };

      axios
        .post(
          that.serviceUrl + "/saisjczd2/cbda2Saisjczd/saveFormData",
          { data: JSON.stringify(tmp) },
          config
        )
        .then((resp) => {
          console.log(resp);
          if (resp.data.success) {
            this.btnLoading = false
            this.pdfParams = resp.data.result;
            this.saveId = resp.data.result.id;
            if (num == 1) {
              that.$message.success("保存成功");
              // localStorage.setItem("menuSelect", "2");
              this.$router.go(-1); // 返回上一个页面
              // that.$router.push("/pc/canszp_saisxz?menuSelect=1");
            } else {
              if (num == 2) {
                this.zuopinExportPdf(resp.data.result);
              } else {
                that.$message.success("暂存成功");
              }
            }
          } else {
            that.$message.error(resp.data.message);
          }
        })
        .catch((error) => {
          this.$message.warning(error);
          //console.log(error);
        });
    },

    async submit(num) {
      if (num == 1) {
        this.$refs["formDataRef"].$refs["ruleForm"].validate((valid) => {
          if (valid) {
            this.submit01(num);
          }
        });
      } else {
        this.submit01(num);
      }
    },
    returnGo() {
      this.$router.go(-1); // 返回上一个页面
    },
  },
};
</script>

<style scoped lang="less">
.but-out {
  position: fixed;
  right: 20px;
  bottom: 20px;
}
</style>
